﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Input;
using System.Windows;
using iTFF.WPF.CP;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using iTFF.WPF.Model.GlobalHotKeys;
using System.Windows.Controls;
using iTFF.WPF.Resources.Languages;

namespace iTFF.WPF.ViewModel
{
    class GlobalHotKeysViewModel : WorkspaceViewModel
    {
        #region Fields

        readonly ControlPanel _cp;
        RelayCommand _applyCommand;
        RelayCommand _cancelCommand;
        bool _isEditingHotKeys;

        #endregion

        #region Contructor

        public GlobalHotKeysViewModel()
        {
            base.DisplayName = Strings.GlobalHotKeys;
            _cp = ControlPanel.Instance;
            HotKeys = new ObservableCollection<GlobalHotKeyViewModel>();

            // load the shortcuts
            _cp.GlobalHotKeysControlPanel.HotKeys.CollectionChanged += HotKeys_CollectionChanged;
            _cp.GlobalHotKeysControlPanel.RestoreGlobalHotKeys();
        }
        #endregion

        #region Properties

        /// <summary>
        /// All the Hot Keys, registered or not
        /// </summary>
        public ObservableCollection<GlobalHotKeyViewModel> HotKeys { get; set; }

        /// <summary>
        /// Indicates if the user is currently editing the HotKeys
        /// </summary>
        public bool IsEditingHotKeys 
        {
            get { return _isEditingHotKeys; }
            set
            {
                if (_isEditingHotKeys != value)
                {
                    _isEditingHotKeys = value;

                    UpdateCanEditForhotKeys();

                    OnPropertyChanged("IsEditingHotKeys");
                }
            }
        }

        private void UpdateCanEditForhotKeys()
        {
            foreach (var hk in HotKeys)
                hk.CanEdit = IsEditingHotKeys;
        }

        #endregion

        #region HotKeys collection management

        void HotKeys_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
        {
            // add the new
            if (e.NewItems != null)
            {
                var i = 0;
                foreach (GlobalHotKey item in e.NewItems)
                    HotKeys.Add(new GlobalHotKeyViewModel(item, e.NewStartingIndex + i++));
            }

            // remove  the old
            if (e.OldItems != null)
            {
                foreach (GlobalHotKey item in e.OldItems)
                    HotKeys.Remove(HotKeys.FirstOrDefault(hk => hk.Name == item.Name));
            }
        }

        #endregion

        #region Commands

        public ICommand ApplyCommand
        {
            get
            {
                if (_applyCommand == null)
                    _applyCommand = new RelayCommand(param => OnApply());
                return _applyCommand;
            }
        }

        /// <summary>
        /// Applies all Hot Keys
        /// </summary>
        void OnApply()
        {
            // apply for all HotKeys
            foreach (var hk in HotKeys)
            {
                if (hk.ApplyCommand.CanExecute(null))
                    hk.ApplyCommand.Execute(null);
            }

            IsEditingHotKeys = false;

            // save the keys
            ControlPanel.Instance.GlobalHotKeysControlPanel.SaveGlobalHotKeys();
        }

        public ICommand CancelCommand
        {
            get
            {
                if (_cancelCommand == null)
                    _cancelCommand = new RelayCommand(param => OnCancel());
                return _cancelCommand;
            }
        }

        /// <summary>
        /// Restore all HotKeys to their previous states
        /// </summary>
        void OnCancel()
        {
            // cancel for all HotKeys
            foreach (var hk in HotKeys)
            {
                if (hk.CancelCommand.CanExecute(null))
                    hk.CancelCommand.Execute(null);
            }

            IsEditingHotKeys = false;
        }

        #endregion

    }
}
